package com.liu.mongodb.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.liu.mongodb.demo.bean.PageVO;
import com.liu.mongodb.demo.entity.Student;
import com.liu.mongodb.demo.response.BasicResponse;
import com.liu.mongodb.demo.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class StudentController {

    @Resource
    StudentService studentService;

    /**
     * 加一条数据
     * @param student
     * @return
     */
    @PostMapping(value = "/addstudent",produces = {"application/json;charset=utf-8"})
    public BasicResponse addStudent(@RequestBody Student student){
        if(!StringUtils.hasLength(student.getName())) {
            return BasicResponse.ERROR("-100", "姓名不能为空");
        }
        if(student.getStudentId() == null) {
            return BasicResponse.ERROR("-101", "学生id不能为空");
        }
        if(student.getId() == null) {
            return BasicResponse.ERROR("-102", "id不能为空");
        }

        Student student1 = studentService.insertStudent(student);
        return BasicResponse.OK("保存成功",student1);
    }

    /**
     * 批量插入
     * @param students
     * @return
     */
    @PostMapping(value = "/addStudents",produces = {"application/json;charset=utf-8"})
    public String addStudents(@RequestBody ArrayList<Student> students){
        ArrayList<Student> students1 = studentService.insertStudents(students);
        return JSON.toJSONString(students1);
    }

    /**
     * 根据id删除对象
     * @param studentId
     * @return
     */
    @PostMapping(value = "/removeSutdent",produces = {"application/json;charset=utf-8"})
    public String removeStudent(Long studentId){
        Student student = studentService.removeStudent(studentId);
        log.info("要删除的对象为：{}",JSON.toJSONString(student));
        return JSON.toJSONString(student);
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @PostMapping(value = "/removeAllStudent",produces = {"application/json;charset=utf-8"})
    public String removeAllStudent(@RequestBody Long[] ids){
        ArrayList<Student> students = studentService.removeAllStudent(ids);
        log.info("要删除的对象为：{}",JSON.toJSONString(students));
        return JSON.toJSONString(students);
    }

    /**
     * 通过save方法更新数据
     * @param student
     * @return
     */
    @PostMapping(value = "/updateStudent",produces = {"application/json;charset=utf-8"})
    public String updateStudent(@RequestBody Student student){
        Student student1 = studentService.updataStudent(student);
        log.info("要更新的对象为：{}",JSON.toJSONString(student1));
        return JSON.toJSONString(student1);
    }

    /**
     * 设置更新数据字段
     * @param student
     * @return
     */
    @PostMapping(value = "/updateStudentByfield",produces = {"application/json;charset=utf-8"})
    public String updateStudentByfield(@RequestBody Student student){
        Student student1 = studentService.updataStudentByfield(student);
        log.info("要更新的对象为：{}",JSON.toJSONString(student1));
        return JSON.toJSONString(student1);
    }

    @GetMapping(value = "/coutStudent",produces = {"application/json;charset=utf-8"})
    public String countStudent(){
        long count = studentService.countOfStudent();
        Map<String,Object> map = new HashMap<>();
        map.put("count",count);
        return JSON.toJSONString(map);
    }

    /**
     * 根据id来查询学生
     * @param id
     * @return
     */
    @GetMapping(value = "/findOneStudentByid",produces = {"application/json;charset=utf-8"})
    public String findOneStudentByid(Long id){
        Student student = studentService.findOneStudent(id);
        log.info("查询的student为{}",student);
        return JSON.toJSONString(student);
    }

    /**
     * 暂定查询住址包括武当山的所有数据
     * @return
     */
    @GetMapping(value = "/findAllStudents",produces = {"application/json;charset=utf-8"})
    public String findAllStudents(){
        List<Student> studentList = studentService.findAllStudent();
        log.info("查询的students为{}",studentList);
        return JSON.toJSONString(studentList);
    }

    /**
     * 模拟分页效果，同时加入了排序
     * @param pageIndex
     * @param pageSize
     * @param addresslike
     * @return
     */
    @GetMapping(value = "/findPageofStudents",produces = {"application/json;charset=utf-8"})
    public String findPageofStudents(Integer pageIndex,Integer pageSize,String addresslike){
        PageVO<Student> pageVO = studentService.findSomeStudent(pageIndex,pageSize,addresslike);
        log.info("分页排序：{}",pageVO);
        return JSON.toJSONString(pageVO);
    }
}
